package com.hziee.graduate.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.graduate.ticket.dao.*;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.entity.Room;
import com.hziee.graduate.ticket.entity.Seat;
import com.hziee.graduate.ticket.entity.SeatSchedule;
import com.hziee.graduate.ticket.service.SeatService;
import com.hziee.graduate.ticket.vo.MovieChartVO;
import com.hziee.graduate.ticket.vo.SeatChartVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("seatService")
public class SeatServiceImpl extends ServiceImpl<SeatDao, Seat> implements SeatService {

    @Resource
    private SeatDao seatDao;

    @Resource
    private RoomDao roomDao;

    @Resource
    private ScheduleDao scheduleDao;

    @Override
    public R<List<Seat>> getSeatByRoom(Long roomId) {
        Map<String, Object> map = new HashMap<String, Object>();
        QueryWrapper<Seat> seatQueryWrapper = new QueryWrapper<Seat>().in("room_id", roomId);
        List<Seat> seats = seatDao.selectList(seatQueryWrapper);
        return R.success(seats);
    }

    @Override
    @Transactional
    public R<List<Seat>> updateSeatStatus(Long roomId, Long rowId, Long colId, Integer seatStatus) {
        // 更新座位
        Seat seat = new Seat();
        seat.setSeatStatus(seatStatus);
        HashMap<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("room_id", roomId);
        queryMap.put("row_id", rowId);
        queryMap.put("col_id", colId);
        UpdateWrapper<Seat> seatUpdateWrapper = new UpdateWrapper<Seat>().allEq(queryMap);
        int update = seatDao.update(seat, seatUpdateWrapper);
        //更新排班座位
//        SeatSchedule seatSchedule = new SeatSchedule();
//        seatSchedule.setSeatStatus(seatStatus);
//        UpdateWrapper<SeatSchedule> seatScheduleUpdateWrapper = new UpdateWrapper<SeatSchedule>().allEq(queryMap);
//        int update1 = seatScheduleDao.update(seatSchedule, seatScheduleUpdateWrapper);
        return R.success("座位更新成功");
    }

    @Override
    public R<List<SeatChartVO>> getSeatChart(Map<String, Object> map) {
        // 需要获取数据的日期
        List<String> dates = (ArrayList<String>) map.get("dates");
        Long cinemaId = Long.parseLong(map.get("cinemaId").toString());
        List<SeatChartVO> res = new ArrayList<>();
        // 获取所有的影院
        HashMap<String, Object> queryMap = new HashMap<>();
        queryMap.put("cinema_id", cinemaId);
        queryMap.put("del_flag", 0);
        List<Room> rooms = roomDao.selectByMap(queryMap);
        // 查找每个影院的票房信息
        rooms.forEach(item -> {
            SeatChartVO seatChartVO = new SeatChartVO();
            seatChartVO.setCinemaName(item.getRoomName());
            dates.forEach(item2 -> {
                Integer done = roomDao.getCheckedTicketByDate(item.getRoomId(), item2);
                Integer scheduleCount = scheduleDao.getScheduleCountByDate(item.getRoomId(), item2);
                if(scheduleCount == 0)  seatChartVO.getDatas().add(0.0);
                else  seatChartVO.getDatas().add(
                       (done * 1.0 / (scheduleCount * item.getRoomContain()))* 100
                );
            });
            res.add(seatChartVO);
        });
        return R.success(res);
    }

}

